#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
using namespace std;

#include <purewell/code/url.hpp>
using namespace purewell;

bool
test_Stream(const char* istr, size_t ilen)
{
	const string is(istr, ilen);
	istringstream iss(is);
	ostringstream oss;
	gPWUrl.encode(oss, iss);

	const string is2(oss.str());
	istringstream iss2(is2);
	ostringstream oss2;
	gPWUrl.decode(oss2, iss2);

	const string is3(oss2.str());
	istringstream iss3(is3);
	ostringstream oss3;
	gPWUrl.encode(oss3, iss3);

	const string is4(oss3.str());

	return !is2.compare(is4);
}

bool
test_Block(const char* ibuf, size_t iblen)
{
	char buf1[iblen*3];
	size_t blen1;
	blen1 = gPWUrl.encode(buf1, ibuf, iblen);

	char buf2[iblen];
	size_t blen2;
	blen2 = gPWUrl.decode(buf2, buf1, blen1);

	char buf3[iblen*3];
	size_t blen3;
	blen3 = gPWUrl.encode(buf3, buf2, blen2);

	char buf4[iblen];
	size_t blen4;
	blen4 = gPWUrl.decode(buf4, buf3, blen3);

	return !memcmp(buf1, buf3, blen1) && !memcmp(buf2, buf4, blen2);
}

int
main(int argc, char* argv[])
{
	char ostr[256];
	for ( size_t i(0); i<sizeof(ostr); i++) ostr[i] = i;
	cout << (test_Stream(ostr, sizeof(ostr))?"success":"failed") << endl;
	cout << (test_Block(ostr, sizeof(ostr))?"success":"failed") << endl;
	return EXIT_SUCCESS;
}

